//
//  CYHomeProjectCollectionViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/16.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYHomeProjectCollectionViewCell.h"
#import "CYPubilishQuestionViewController.h"

@implementation CYHomeProjectCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backView.layer.cornerRadius = 3;
    self.backView.layer.borderColor = RGBOF(0xDDDDDD).CGColor;
    self.backView.layer.borderWidth = 1;
    self.backView.layer.masksToBounds = YES;
    
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
    
    self.doneBtn.layer.cornerRadius = 5;
    self.doneBtn.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeadImageView:)];
    [self.headImageView addGestureRecognizer:tap];
    self.headImageView.userInteractionEnabled = YES;
}

- (void)tapHeadImageView:(UITapGestureRecognizer *)tap{
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    if (self.model) {
        if (self.model.user_id) {
            zhuye.userId = self.model.user_id;
        }else{
            zhuye.userId = self.model.userId;
        }
    }
    if (self.daoshomodel) {
        if (self.daoshomodel.user_id) {
            zhuye.userId = self.daoshomodel.user_id;
        }else{
            zhuye.userId = self.daoshomodel.userId;
        }
    }
    
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}


- (IBAction)doneAction:(id)sender {
    if ([self.doneBtn.titleLabel.text isEqualToString:@"向他提问"]) {
        //提问
        CYPubilishQuestionViewController *tiwen = [[CYPubilishQuestionViewController alloc] init];
        tiwen.type = TiWenType_XiangTaTiWen;
        tiwen.nickname = self.daoshomodel.nickname;
        [[AppDelegate sharedAppDelegate] pushViewController:tiwen];
        
    }else{
        //免费咨询
        if ([self.model.is_follow integerValue] == 1) {
            CYChatViewController *chat = [[CYChatViewController alloc] init];
            chat.url = [NSString stringWithFormat:@"%@?device=ios",self.model.wsurl];
            [[AppDelegate sharedAppDelegate] pushViewController:chat];
        }else{
            //guanzhu
            [self guanzhu];
        }
        
    }
}

- (void)guanzhu{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":self.model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"添加关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_follow = [NSNumber numberWithInteger:1];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}


- (void)setModel:(CYProjectModel *)model{
    _model = model;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.titleLabel.text = model.brand;

    NSString *money = @"";
    if ([model.funds isEqualToString:@"0"]) {
        money = @"全部";
    }else if ([model.funds isEqualToString:@"10"]){
        money = @"3万以下";
    }else if ([model.funds isEqualToString:@"15"]){
        money = @"3-8万";
    }else if ([model.funds isEqualToString:@"5"]){
        money = @"5-10万";
    }else if ([model.funds isEqualToString:@"6"]){
        money = @"10-20万";
    }else if ([model.funds isEqualToString:@"11"]){
        money = @"20万以上";
    }
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"金额:%@", money]];
    [attStr addAttribute:NSForegroundColorAttributeName value:RGBOF(0x999999) range:NSMakeRange(0, 3)];
    [attStr addAttribute:NSForegroundColorAttributeName value:RGBOF(0xF43636) range:NSMakeRange(3, money.length)];

    self.moneyLabel.attributedText = attStr;

    if ([model.is_follow integerValue] == 1) {
        [self.doneBtn setTitle:@"免费咨询" forState:UIControlStateNormal];
    }else{
        [self.doneBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
}

- (void)setDaoshomodel:(CYProjectModel *)daoshomodel{
    _daoshomodel = daoshomodel;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:daoshomodel.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.titleLabel.text = daoshomodel.brand;
    
    self.moneyLabel.text = @"创业咨询导师";
    [self.doneBtn setTitle:@"向他提问" forState:UIControlStateNormal];
}
@end
